package com.krdz.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.krdz.bean.UserBean;
import com.krdz.service.UserService;
/**
 * 用户管理控制器类
 * @author dinghao
 *
 * @date  2018年11月19日
 */
@Controller
@RequestMapping("user")
public class UserController {
	
	@Resource
	private UserService userService;
	
	//获取用户基本信息
	@RequestMapping(value="/findUser",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String getUserInfo(){
		List<UserBean> li=userService.findUserInfo();
		String json= JSONObject.toJSONString(li);
		return json;
	}
	//获取ID查询用户基本信息
	@RequestMapping(value="/findUserById",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String getUserById(@RequestParam("id")Integer id){
		List<UserBean> li=userService.findUserById(id);
		String json= JSONObject.toJSONString(li);
		return json;
	}
	//新增用户信息
	@RequestMapping(value="/addUser",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String addUser(UserBean user){
		if(userService.add(user)){
			return "success";
		}else{
			return "error";
		}
	}
	//修改用户信息
	@RequestMapping(value="/updateUser",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String updateUser(UserBean user){
		if(userService.updateUser(user)){
			return "success";
		}else{
			return "error";
		}
	}
	//删除用户信息
	@RequestMapping(value="/deleteUser",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String deleteUser(@RequestParam("id")Integer id){
		System.out.println(id);
		if(userService.deleteUser(id)){
			return "success";
		}else{
			return "error";
		}
	}
	//头像上传	
	@RequestMapping(value="/upload", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String uploadTest(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request){
		Map<String,Object> result = userService.uploadTest(file, request);
		JSONObject json = new JSONObject(result);
		return json.toString();
	}
	
	//获用户总记录数
	@RequestMapping(value="/findUserCount",produces="application/json;charset=UTF-8")
	@ResponseBody	
	public String getUserCount(){
		Integer count=userService.findUserCount();

		return count.toString();
	}
}
